import { Body, Controller, Delete, Get, HttpStatus, Param, Post, Put, Query, Res } from '@nestjs/common';
import { CreateRoleDto } from './dto/create-role.dto';
import { FindRoleDto } from './dto/find-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { RoleService } from './role.service';

@Controller('role')
export class RoleController {

  constructor(private roleService: RoleService) {
  }

  @Post()
  async create(@Res() res, @Body() body: CreateRoleDto) {
    await this.roleService.create(body);
    return res.status(HttpStatus.CREATED).send({
      status: 'success',
    });
  }

  @Delete(':id')
  async delete(@Param('id') id: number) {
    await this.roleService.delete(id);
    return { status: 'success' };
  }

  @Put(':id')
  async update(@Param('id') id: number, @Body() body: UpdateRoleDto) {
    await this.roleService.update(id, body);
    return { status: 'success' };
  }

  @Get()
  async findAll(@Query() findRoleDto: FindRoleDto) {
    findRoleDto.pn = Math.abs(Number(findRoleDto.pn));
    findRoleDto.ps = Math.abs(Number(findRoleDto.ps));
    return await this.roleService.findAll(findRoleDto);
  }
}